import React, { Component } from 'react';
import { Form, Row, Col, Icon } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sParentId: '', /* 物料分类(*) */
      sMaterialsName: '', /* 物料名称(*) */
      sMaterialsShort: '', /* 型号 */
      sMemo: '', /* 备注 */
      sMaterialsNo: '', /* 物料编号 */
      sMaterialsUnit: '', /* 换算单位(*) */
      dGramWeight: '', /* 克重 */
      bPurchase: '', /* 采购物料 */
      bReel: '', /* 卷筒物料 */
      bSales: '', /* 销售物料 */
      sMaterialsType: '', /* 物料类别 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      reserveContent: '',
      expand: false,
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/elematerials', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    } else if (sId === '' && this.props.newIdFlag) {
      const url = `${config.server_host}getUuid/${this.props.newIdlength}`;
      dispatch({
        type: 'content/getCopyDataId',
        payload: {
          sTabId, url, formRoute: '/elematerials', componentName,
        },
      });
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sParentId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sparentid'))[0],
        sMaterialsName: mainConfig.filter(item => (item.sName.toLowerCase() === 'smaterialsname'))[0],
        sMaterialsNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smaterialsno'))[0],
        sMaterialsShort: mainConfig.filter(item => (item.sName.toLowerCase() === 'smaterialsshort'))[0],
        sMaterialsUnit: mainConfig.filter(item => (item.sName.toLowerCase() === 'smaterialsunit'))[0],
        sMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smemo'))[0],
        dGramWeight: mainConfig.filter(item => (item.sName.toLowerCase() === 'dgramweight'))[0],
        bPurchase: mainConfig.filter(item => (item.sName.toLowerCase() === 'bpurchase'))[0],
        bReel: mainConfig.filter(item => (item.sName.toLowerCase() === 'breel'))[0],
        bSales: mainConfig.filter(item => (item.sName.toLowerCase() === 'bsales'))[0],
        sMaterialsType: mainConfig.filter(item => (item.sName.toLowerCase() === 'smaterialstype'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
    const mainConfig = this.state.masterConfig.gdsconfigformslave;
    if (Object.keys(mainConfig).length > 0) {
      const { sActiveId, app, form } = nextProps;
      const childProps = {
        app, form, sActiveId, sId, dataList, disabled,
      };
      const reserveContent = commonFunc.Reserve(mainConfig, childProps);
      this.setState({ reserveContent });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const checkformItemLayout = { labelCol: { span: 4 }, wrapperCol: { span: 20 } };
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      sParentId, sMaterialsName, sMaterialsNo, sMaterialsShort,
    } = this.state;
    const {
      sMaterialsUnit, sMemo, dGramWeight, bPurchase, bReel,
    } = this.state;
    const { bSales, sMaterialsType } = this.state;
    const {
      dataList, sId, sActiveId, disabled, reserveContent, expand,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const sParentIdProps = { showConfig: sParentId, ...childProps };
    const sMaterialsNameProps = { showConfig: sMaterialsName, ...childProps };
    const sMaterialsNoProps = { showConfig: sMaterialsNo, ...childProps };
    const sMaterialsShortProps = { showConfig: sMaterialsShort, ...childProps };
    const sMaterialsUnitProps = { showConfig: sMaterialsUnit, ...childProps };
    const sMemoProps = { showConfig: sMemo, ...childProps };
    const dGramWeightProps = { showConfig: dGramWeight, ...childProps };
    const bPurchaseProps = { showConfig: bPurchase, ...childProps };
    const bReelProps = { showConfig: bReel, ...childProps };
    const bSalesProps = { showConfig: bSales, ...childProps };
    const sMaterialsTypeProps = { showConfig: sMaterialsType, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex" style={{ height: expand ? 'auto' : '100px', overflow: 'hidden' }}>
          <Col
            span={6}
            order={sParentId.iOrder > 24 ? 24 : sParentId.iOrder}
            style={{ display: sParentId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sParentId" showDefaultName="物料分类" {...sParentIdProps} />
          </Col>
          <Col
            span={6}
            order={sMaterialsName.iOrder > 24 ? 24 : sMaterialsName.iOrder}
            style={{ display: sMaterialsName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMaterialsName" showDefaultName="物料名称" {...sMaterialsNameProps} />
          </Col>
          <Col
            span={6}
            order={sMaterialsShort.iOrder > 24 ? 24 : sMaterialsShort.iOrder}
            style={{ display: sMaterialsShort.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMaterialsShort" showDefaultName="型号" {...sMaterialsShortProps} />
          </Col>
          <Col
            span={6}
            order={sMemo.iOrder > 24 ? 24 : sMemo.iOrder}
            style={{ display: sMemo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMemo" showDefaultName="备注" {...sMemoProps} />
          </Col>
          <Col
            span={6}
            order={sMaterialsNo.iOrder > 24 ? 24 : sMaterialsNo.iOrder}
            style={{ display: sMaterialsNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMaterialsNo" showDefaultName="物料编号" {...sMaterialsNoProps} />
          </Col>
          <Col
            span={6}
            order={sMaterialsUnit.iOrder > 24 ? 24 : sMaterialsUnit.iOrder}
            style={{ display: sMaterialsUnit.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMaterialsUnit" showDefaultName="换算单位" {...sMaterialsUnitProps} />
          </Col>
          <Col
            span={6}
            order={dGramWeight.iOrder > 24 ? 24 : dGramWeight.iOrder}
            style={{ display: dGramWeight.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dGramWeight" showDefaultName="克重" {...dGramWeightProps} />
          </Col>
          <Col
            span={6}
            order={sMaterialsType.iOrder > 24 ? 24 : sMaterialsType.iOrder}
            style={{ display: sMaterialsType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMaterialsType" showDefaultName="物料类别" {...sMaterialsTypeProps} />
          </Col>
          <Col
            span={6}
            order={bPurchase.iOrder > 24 ? 24 : bPurchase.iOrder}
            style={{ display: bPurchase.bVisible ? 'block' : 'none' }}
          >
            <Row>
              <Col span={7}>
                <ShowType sName="bPurchase" showDefaultName="采购物料" {...bPurchaseProps} formItemLayout={checkformItemLayout} />
              </Col>
              <Col span={7}>
                <ShowType sName="bSales" showDefaultName="销售物料" {...bSalesProps} formItemLayout={checkformItemLayout} />
              </Col>
              <Col span={7}>
                <ShowType sName="bReel" showDefaultName="卷筒物料" {...bReelProps} formItemLayout={checkformItemLayout} />
              </Col>
            </Row>
          </Col>
          {reserveContent}
        </Row>
        <button className={styles.collapseA} onClick={this.handleToggle}>
          <span>{expand ? '缩略展示' : '显示全部'}</span><Icon type={expand ? 'up' : 'down'} />
        </button>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
